package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"server/internal/model"
	"strconv"
)

func GetTool(c *gin.Context) {
	tool := &model.Tool{}
	father := c.Query("father")
	if len(father) == 0 {
		father = "0"
	}
	father_id, err := strconv.Atoi(father)
	if err != nil {
		c.JSON(http.StatusOK, []model.ToolMeau{})
		return
	}
	all_tools := tool.List(father_id)
	c.JSON(http.StatusOK, all_tools)
}

func CreateTool(c *gin.Context) {
	userinfo := GetUser(c)
	_ = c.Request.ParseForm()
	tool := &model.Tool{}
	_ = c.ShouldBind(tool)
	if len(tool.Name) == 0 {
		c.String(http.StatusOK, "接口名为空")
		return
	}
	go CreateLog(c, "API接口", "创建接口"+tool.Name)

	tool.OwnerID = userinfo.ID
	err := tool.Create()
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "任务创建成功")
}

func UpdateTool(c *gin.Context) {
	meau := &model.Tool{}
	_ = c.ShouldBind(meau)
	if err := meau.Update(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "工具修改成功")
}

func DeleteTool(c *gin.Context) {
	meau := &model.Tool{}
	meau.Name = c.Param("name")
	if err := meau.Delete(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "工具删除成功")
}
